//
//  TopicViewController.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView    *TopicTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;// 数据数组

@end
@interface TopicViewController ()


@end

@implementation TopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor                         = [UIColor cyanColor];
    self.navigationItem.rightBarButtonItem            = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(sousuo:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // tableView
    [self creatTableView];
    // 数据
    [self dataHandler];
    //下拉刷新
    MJRefreshNormalHeader *header                     = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self dataHandler];
        [self.TopicTableView.mj_header endRefreshing];
    }];
    
    self.tabBarController.tabBar.hidden = YES;
    //刷新列表
    //设置刷新控件
    self.TopicTableView.mj_header                     = header;
    //设置文字
    [header setTitle:@"用力!" forState:MJRefreshStateIdle];
    [header setTitle:@"行了!" forState:MJRefreshStatePulling];
    [header setTitle:@"等！！！" forState:MJRefreshStateRefreshing];
    header.stateLabel.font                            = [UIFont systemFontOfSize:20];
    
    MJRefreshAutoFooter *footer                       = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self dataFooter];
        [self.TopicTableView.mj_footer endRefreshing];
    }];
    self.TopicTableView.mj_footer                     = footer;

    
    
}
// 搜索
- (void)sousuo:(UIBarButtonItem *)sousuo
{
//    SouSuoViewController *souSuo = [[SouSuoViewController alloc] init];
//    [self.navigationController pushViewController:souSuo animated:YES];
  
}
// 当页面出现时导航栏出现
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha = 1;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)dataFooter
{
    static NSInteger t = 20;
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/list/expert/0-%ld.html", t];
    [NewWorking GetDayaWithURL:str dic:nil sussess:^(id responseObject) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [TopicModel modelHanderWithArray:responseObject[@"data"][@"expertList"]];
        [self.TopicTableView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    t += 10;
}

// 数据请求
- (void)dataHandler
{
    NSString *str = @"http://c.m.163.com/newstopic/list/expert/0-10.html";
    [NewWorking GetDayaWithURL:str dic:nil sussess:^(id responseObject) {
        self.dataArr = [NSMutableArray array];
        self.dataArr = [TopicModel modelHanderWithArray:responseObject[@"data"][@"expertList"]];
        
        [self.TopicTableView reloadData];
    } filed:^(NSError *error) {
        
    }];
    
    
}

- (void)creatTableView
{
    // 创建tableView
    self.TopicTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    // 签订协议
    _TopicTableView.delegate = self;
    _TopicTableView.dataSource = self;
    // 右侧滑动线隐藏
    _TopicTableView.showsVerticalScrollIndicator = NO;
    // cell分割线隐藏
    _TopicTableView.separatorStyle = NO;
    // 设置偏移量
    _TopicTableView.contentInset = UIEdgeInsetsMake(0, 0, 110 * KWITH, 0);
    [self.view addSubview:_TopicTableView];
    // 注册重用池
    [_TopicTableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"cell"];
  
}

#pragma mark - tableView协议方法的实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicModel *t = _dataArr[indexPath.row];
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   // [cell NightWithType:UIViewColorTypeNormal];
    
    [cell.image  sd_setImageWithURL:[NSURL URLWithString:t.picurl]];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:t.headpicurl]];
    cell.name.text = t.name;
    cell.title.text = t.alias;
//    [cell.title NightWithType:UIViewColorTypeNormal];
    
    cell.comment.text = [NSString stringWithFormat:@"%@关注|", t.concernCount];
//    [cell.guanzhu NightWithType:UIViewColorTypeNormal];
    
    cell.kind.text = t.classification;
//    [cell. NightWithType:UIViewColorTypeNormal];
    
    cell.state.text = @"|进行中";
//    [cell.zhuangtai NightWithType:UIViewColorTypeNormal];
    
    return cell;
}

#pragma mark - cell的高度设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 290 * KHIGHT;
}

#pragma mark - cell的点击方法

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicModel *t = _dataArr[indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
     //详情页面
  TopicPingViewController *TPVC = [[TopicPingViewController alloc] init];
    // 传递id
    TPVC.titleStr = t.alias;
    TPVC.idStr = t.expertId;
    [self.navigationController pushViewController:TPVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
